<template>
    <div class="courseCircumstance">
      <div v-if="loadingStatus === 1">
        <div class="rate">
          <div class="star">
            <img v-for="(star, starIndex) in 3" :key="starIndex" :src="courseData.all_start >= star+1? 'https://mobuz-mp.cdn.bcebos.com/matter/star_fill@2x.png' :'https://mobuz-mp.cdn.bcebos.com/matter/star_empty@2x.png'" alt="">
          </div>
          <div class="score">得分 {{courseData.all_score}}</div>
          <div class="clock">
            <!-- <div class="clockBorder">
              <div class="clockIcon"></div>
            </div> -->
            <div class="clockText" v-if="courseData.clock_state !== '1'">未打卡</div>
            <div class="clockText" v-if="courseData.clock_state === '1'">已打卡</div>
          </div>
        </div>
        <div class="section-content">
          <div class="section-title">
            <div class="day">Day {{courseData.section_day < 10 ? '0' + courseData.section_day : courseData.section_day }}</div>
            <div class="title">{{courseData.section_name}}</div>
          </div>
          <!-- 课程内容 0 - 文本   1 - 图片   2 - 音频   3 - 视屏   4 - 作业   5 - 游戏 -->
          <div class="section-step" v-for="(item, index) in courseData.section_content" :key="item+index" v-if="item.status === 0 || item.status === 4 || item.status === 5">
            <!-- <div class="goto-btn" v-if="item.status === 4" style="margin-top: 40rpx">打开悦器社APP开始学习</div> -->
            <div class="step-name" v-if="item.name && item.status !== 4">
              {{item.name}}
            </div>
            <div class="step-name" v-if="item.name && item.status === 4">
              提交作业
            </div>
            
            <div class="job-module" v-if="item.status === 4 && item.image && item.image.length">
              <div class="music-score" :style="{'backgroundImage':'url('+item.image[0]+')'}" @click="showMusicScore(item.image)"></div>
            </div>
            <div class="step-content" v-if="item.status !== 1 && item.status !== 5">
              <!-- {{item.content}} -->
              <P class="stepCache" v-for="(itemText, indexText) in item.cache" :key="indexText">{{itemText}}</P>
              <div class="goto-btn" v-if="item.status === 4" style="margin-top: 30rpx" @click.stop.prevent="goToCommentJob">提交作业</div>
            </div>
            <div class="step-cache" v-if="item.cache && item.cache.length && item.status === 5">
              <div class="cache-item" v-for="(cache, cIndex) in item.cache" :key="cache+cIndex">
                <div class="clock">
                  <div class="clockIcon" v-if="cache.cleared === 1">
                    <img src="https://mobuz-mp.cdn.bcebos.com/matter/complete_02@2x.png" lazy-load>
                  </div>
                </div>
                <div class="icon">
                  <!-- 练习条类型:0视频1乐谱2问答3教学视频 -->
                  <img v-if="cache.stripType === 1 && cache.follow === '0'" src="https://mobuz-mp.cdn.bcebos.com/matter/game_music_black@2x.png" lazy-load>
                  <img v-if="cache.stripType === 1 && cache.follow === '1'" src="https://mobuz-mp.cdn.bcebos.com/matter/game_play_black@2x.png" lazy-load>
                  <img v-if="cache.stripType === 0 || cache.stripType === 3" src="https://mobuz-mp.cdn.bcebos.com/matter/game_video_black@2x.png" lazy-load>
                  <img v-if="cache.stripType === 2" src="https://mobuz-mp.cdn.bcebos.com/matter/game_wenda_black@2x.png" lazy-load>
                </div>
                <div class="title">{{cache.title}}</div>
                <div class="rate-state">
                  <!-- :style="{visibility: (cache.stripType !== 0 && cache.stripType !== 3)? 'hidden': ''}" -->
                  <div class="star" v-if="cache.stripType !== 0 && cache.stripType !== 3">
                    <img v-for="(star, starIndex) in 3" :key="starIndex" :src="cache.star >= star+1? 'https://mobuz-mp.cdn.bcebos.com/matter/star_fill@2x.png' :'https://mobuz-mp.cdn.bcebos.com/matter/star_empty@2x.png'" alt="">
                  </div>
                  <div class="score" v-if="cache.stripType === 1 && cache.follow === '0'">得分 {{cache.score ? cache.score : '---'}}</div>
                </div>
              </div>
            </div>
            <div class="goto-btn" v-if="item.status === 5 && lastLevelIndex === index" style="margin-top: 40rpx">
              打开悦器社APP开始学习
              <!-- :app-parameter="union_id=`${union_id}`&student_id=${student_id}&batch_id=${batch_id}&section_id=${section_id}" -->
              <!-- :app-parameter="'union_id='+union_id" -->
              <button class="launchAppButton" :app-parameter="'union_id='+union_id+'&student_id='+student_id+'&batch_id='+batch_id+'&section_id='+section_id" open-type="launchApp"  @error="launchAppError"></button> 
            </div>
          </div>
          <!-- <div class="goto-btn" v-if="layoutTask">打开悦器社APP开始学习</div> -->
          <!-- 讲堂 -->
          <div class="lecture">
            <div class="lecture-title">
              小讲堂
              <span class="lecture-depict">（难点总结，趣味知识分享，每天21点更新）</span>
            </div>
            <div class="lecture-banner" v-if="!courseData.type_lesson"></div>
            <div class="lecture-lesson" v-if="courseData.type_lesson" @click.stop.prevent="goToCourseAuditorium">
              <img :src="courseData.type_lesson" lazy-load  mode="widthFix">
            </div>
          </div>
        </div>
      </div>
      <div class="backHome" v-if="terrace_state == '2'" @click.stop.prevent="goToWXHome">
        <img src="https://mobuz-mp.cdn.bcebos.com/matter/back_home_page.png" lazy-load  mode="widthFix">
      </div>
    </div>
</template>

<script>
import {
  obtainCourseParticulars,
  judgeTaskRepetition
} from '@/config/api';
import {} from '@/config/mUtils';
import {showMessager, showPictureCard} from '@/config/wUtils';

export default {
  components:{
  },
  data () {
    return {
      // 课程数据
      courseData: {},
      // 最后一个练习模块 下标
      lastLevelIndex: null,

      // 加载状态 0 --- 加载前  1 --- 加载成功 2 --- 加载失败
      loadingStatus: 0,

      // 平台标识  1-小程序2-app
      terrace_state: 1,

      // 平台唯一凭证id
      union_id: '',
      // 学员id
      student_id: '',
      // 批次id
      batch_id: '',
      // 单节id
      section_id: ''
    }
  },
  mounted () {
    // 初始化 课程详情界面
    this.initCourseParticulars()
  },
  methods: {
    // 初始化 课程详情界面
    initCourseParticulars() {
      // this.section_id = '20181016004001001';
      // this.batch_id = '20181107015';
      // this.union_id = 'oFulp0k0cSxiBYrqTxq6n7skHGH8';
      // this.student_id = wx.getStorageSync('student_id');
      // this.courseParticulars()
      
      // this.batch_id = wx.getStorageSync('batch_id');
      // this.section_id = '20180824003002008';
      
      // section_id, batch_id, union_id

      // 平台标识  1-小程序2-app
      if (this.$root.$mp.query.terrace_state && this.$root.$mp.query.terrace_state === '2') {
        console.log('平台标识  1-小程序 2-app')
        // 平台标识  1-小程序2-app
        this.terrace_state = this.$root.$mp.query.terrace_state
        if (this.$root.$mp.query.section_id && this.$root.$mp.query.batch_id) {
          console.log(this.$root.$mp.query, '......this.$root.$mp.query')
          this.section_id = this.$root.$mp.query.section_id
          this.batch_id = this.$root.$mp.query.batch_id
          const userInfo = wx.getStorageSync('userInfo');
          if (this.$root.$mp.query.section_id && JSON.stringify(userInfo) != '[]') {
            this.union_id = userInfo.unionId
          }
          this.student_id = this.$root.$mp.query.student_id || ''
          this.courseParticulars()
        } else {
          showMessager('参数错误')
        }
      } else {
        const userInfo = wx.getStorageSync('userInfo');
        // 平台标识  1-小程序2-app
        this.terrace_state = '1'
        if (this.$root.$mp.query.section_id && JSON.stringify(userInfo) != '[]') {
          this.section_id = this.$root.$mp.query.section_id
          this.batch_id = wx.getStorageSync('batch_id');
          this.union_id = userInfo.unionId;
          this.student_id = wx.getStorageSync('student_id');
          this.courseParticulars()
        }   
      }
    },
    // 获取 课程详情数据
    async courseParticulars () {
      wx.showNavigationBarLoading()
      this.courseData = []
      // 加载状态 0 --- 加载前  1 --- 加载成功 2 --- 加载失败
      this.loadingStatus = 0
      let data = await obtainCourseParticulars(this.section_id,this.batch_id,this.union_id,this.student_id, 1)
      console.log(data,'..........new 课程详情')
      if (data.status === 200) {
        this.courseData = data.data;
        if (JSON.stringify(this.courseData.section_content) !== '[]') {
          for (let i=0; i<this.courseData.section_content.length; i++) {
            if (this.courseData.section_content[i].status === 5) {
              // 最后一个练习模块 下标
              this.lastLevelIndex = i
            }
          }
        }
        // 加载状态 0 --- 加载前  1 --- 加载成功 2 --- 加载失败
        this.loadingStatus = 1
      } else {
        // 加载状态 0 --- 加载前  1 --- 加载成功 2 --- 加载失败
        this.loadingStatus = 2
      }
      wx.hideNavigationBarLoading()
      wx.stopPullDownRefresh()
    },
    // 显示曲谱
    showMusicScore (imgs) {
      console.log(imgs)
      showPictureCard(imgs[0],imgs)
    },
    // 跳转到 编辑作业界面
    async goToCommentJob() {
      let self = this;
      console.log('跳转到 编辑作业界面')
      if (wx.getStorageSync("student_id")) {
        // 判断是否重复提交
        const redoInfo = await judgeTaskRepetition(self.$root.$mp.query.section_id, wx.getStorageSync("student_id"))
        console.log(redoInfo, '.........redoInfo')
        if (redoInfo.status === 200) {
          wx.navigateTo({
            url: '../sendJob/main?section_id='+self.$root.$mp.query.section_id
          })
        }else {
          showMessager(redoInfo.msg)
        }
      }
    },
    // 跳转 app 失败
    launchAppError(e) {
      let self = this;
      console.log(e.mp.detail.errMsg)
      if (e.mp.detail.errMsg === 'invalid scene'){
        // showMessager('跳转 app 失败')
        wx.showModal({
          title: '',
          content: '请到应用商店下载最新版本悦器社，如果您已经下载了app，请尝试其他链接跳转（此链接已过期）',
          success(res) {}
        })
      }
    },
    // 跳转到 小讲堂
    goToCourseAuditorium() {
      console.log('跳转到 小讲堂')
      let self = this;
      if (self.$root.$mp.query.section_id && self.batch_id) {
        wx.navigateTo({
          url: '../courseAuditorium/main?section_id='+this.section_id+'&batch_id='+self.batch_id
        })
      }
    },
    // 跳转到 小程序 首页
    goToWXHome() {
      let self = this;
      wx.switchTab({ url: "../dengesCourse/main" });
    }

  },
  // 下拉刷新
  onPullDownRefresh () {
    // 初始化 课程详情界面
    this.initCourseParticulars()
  },
  // 上拉加载
  onReachBottom () {
  },
  // 切换界面
  onHide () {
  },
  // 滚动界面
  onPageScroll (res) {
  },
  // 页面加载
  onLoad () {
  },
  // 页面显示
  onShow () {
    // console.log(this.$root.$mp.query, '......this.$root.$mp.query')
    // // 初始化 课程详情界面
    // this.initCourseParticulars()
  }
}
</script>

<style lang="less" scoped>
.courseCircumstance {
  padding-bottom: 50rpx;
  .rate {
    position: fixed;
    width: 100%;
    top: 0;
    left: 0;
    height: 70rpx;
    background-color: #fff;
    z-index: 99;
    > div{
      height: 70rpx;
      position: absolute;
      &.star {
        left: 32rpx;
        width: 128rpx;
        font-size: 0;
        vertical-align:  middle;
        img {
          width: 30rpx;
          height: 30rpx;
          margin-right: 10rpx;
          vertical-align:  middle;
        }
      }
      &.score{
        font-family: PingFangSC-Regular;
        font-size: 30rpx;
        color: #333333;
      }
      &.clock {
        font-size: 0;
        .clockBorder{
          display: inline-block;
          vertical-align: middle;
          width: 30rpx;
          height: 30rpx;
          border-radius: 50%;
          border: 1rpx solid #ee3a29;
          box-sizing: border-box;
          position: relative;
          .clockIcon{
            position: absolute;
            img{
              width: 100%;
              height: 100%;
            }
          }
        }
        .clockText{
          display: inline-block;
          vertical-align: middle;
          font-family: PingFangSC-Regular;
          font-size: 30rpx;
          color: #333333;
        }
      }
      &.score {
        left: 0;
        width: 100%;
        text-align: center;
      }
      &.clock {
        right: 32rpx;
      }
    }
    
  }
  .section-content {
    padding: 80rpx 32rpx 0;
    .section-title {
      // padding-bottom: 32rpx;
      font-family: PingFangSC-Medium;
      font-size: 36rpx;
      color: #333333;
      letter-spacing: 1.2rpx;
      line-height: 50rpx;
      .day {
        font-weight: 600;
      }
    }
    .section-step {
      // padding-bottom: 32rpx;
      .step-name {
        padding-left: 11rpx;
        height: 28rpx;
        font-family: PingFangSC-Medium;
        font-size: 30rpx;
        color: #333333;
        letter-spacing: 1rpx;
        line-height: 1;
        position: relative;
        margin-top: 32rpx;
        &::before {
          content: '';
          width: 2px;
          height: 28rpx;
          position: absolute;
          left: 0;
          top: 0;
          background-color: #EE3a29;
        }
      }
      .step-content {
        padding: 15rpx 0 0rpx;
        font-family: PingFangSC-Regular;
        font-size: 28rpx;
        color: #666666;
        letter-spacing: 1.5rpx;
        line-height: 40rpx;
        // word-wrap:break-word
        .stepCache{
          word-wrap:break-word;
          text-align: justify;
        }
      }
      .step-cache {
        margin-top: 30rpx;
        background: #FFFFFF;
        box-shadow: 0 4rpx 15rpx 3rpx rgba(224,224,224,0.50);
        border-radius: 10rpx;
        padding: 32rpx 25rpx;
        .cache-item {
          position: relative;
          font-size: 0;
          &:nth-child(3n+1){
            .icon {
              width: 22rpx;
              height: 20rpx;
              margin-right: 9rpx;
            }
          }
          &:nth-child(3n+2){
            .icon {
              width: 22rpx;
              height: 19rpx;
              margin-right: 8rpx;
            }
          }
          &:nth-child(3n+3){
            .icon {
              width: 20rpx;
              height: 20rpx;
              margin-right: 8rpx;
            }
          }
          & + .cache-item {
            margin-top: 32rpx;
          }
          > div {
            display: inline-block;
            vertical-align: middle
          }
          .clock {
            width: 24rpx;
            height: 24rpx;
            border-radius: 50%;
            // background: #EE3a29;
            // border: 1rpx solid #ee3a29;
            box-sizing: border-box;
            margin-right: 10rpx;
            position: relative;
            .clockIcon{
              width: 100%;
              height: 100%;
              position: absolute;
              top: 0;
              left: 0;
              img{
                width: 100%;
                height: 100%;
              }
            }
          }
          .icon {
            img{
              width: 100%;
              height: 100%;
            }
          }
          .title {
            font-family: PingFangSC-Regular;
            font-size: 24rpx;
            color: #666666;
            letter-spacing: 0.57rpx;
            width: 40%;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            -o-text-overflow: ellipsis;
          }
          .rate-state {
            position: absolute;
            right: 0;
            width:290rpx;
            .star {
              // left: 32rpx;
              display: inline-block;
              width: 98rpx;
              font-size: 0;
              vertical-align:  middle;
              margin-right: 24rpx;
              img {
                width: 26rpx;
                height: 26rpx;
                margin-right: 10rpx;
                vertical-align:  middle;
                &:last-child{
                  margin-right: 0;
                }
              }
            }
            .score {
              width: 169rpx;
              font-family: PingFangSC-Regular;
              font-size: 24rpx;
              color: #666666;
              letter-spacing: 0.57rpx;
              white-space: nowrap;
              overflow: hidden;
              text-overflow: ellipsis;
              -o-text-overflow: ellipsis;
              display: inline-block;
              vertical-align:  middle;
            }
          }
        }
      }
    }
    .job-module {
      padding: 15rpx 0 20rpx;
      .music-score {
        height: 350rpx;
        background-repeat: no-repeat;
        background-size: cover;
      }
      .job-title {
        font-family: PingFangSC-Regular;
        font-size: 28rx;
        color: #666666;
        letter-spacing: 1.5rpx;
        line-height: 40rpx;
      }
    }
    .goto-btn {
      // margin-bottom: 32rpx;
      height: 70rpx;
      line-height: 70rpx;
      font-family: PingFangSC-Regular;
      font-size: 28rpx;
      color: #FFFFFF;
      letter-spacing: 0.44rpx;
      text-align: center;
      background-image: linear-gradient(-90deg, #FE7A72 0%, #FFB497 99%);
      border-radius: 35rpx;
      position: relative;
      .launchAppButton{
        position: absolute;
        top: 0;
        width: 100%;
        height: 100%;
        opacity: 0;
      }
    }
    .lecture {
      width: 100%;
      margin-top: 32rpx;
      .lecture-title {
        padding-left: 11rpx;
        margin-bottom: 15rpx;
        line-height: 1;
        font-family: PingFangSC-Medium;
        font-size: 30rpx;
        color: #333333;
        letter-spacing: 1rpx;
        position: relative;
        &::before {
          content: '';
          width: 2px;
          height: 28rpx;
          position: absolute;
          left: 0;
          top: 0;
          background-color: #EE3a29;
        }
        .lecture-depict{
          // width: 494rpx;
          height: 33rpx;
          font-size: 24rpx;
          font-family: PingFangSC-Regular;
          font-weight: 400;
          color: rgba(102,102,102,1);
          line-height: 1;
          display: inline-block;
          vertical-align: middle;
        }
      }
      .lecture-banner {
        height: 300rpx;
        background: #F1F1F1;
        border-radius: 10px;
      }
      .lecture-lesson{
        width: 100%;
        height: auto;
        border-radius: 10px;
        overflow: hidden;
        img{
          width: 100%;
        }
      }
    }
  }
  .backHome{
    position: fixed;
    top: 865rpx;
    right: 25rpx;
    width: 82rpx;
    height: 82rpx;
    img{
      width: 100%;
      height: 100%;
    }
  }
}
</style>
